package com.epalmpay.service.common.impl;

import com.epalmpay.dto.mall.CitiesOutDTO;
import com.epalmpay.dto.mall.CityOutDTO;
import com.epalmpay.entity.Cities;
import com.epalmpay.mapper.CitiesMapper;
import com.epalmpay.service.common.ICitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by lifayi on 2017/8/4.
 */
@Service
public class CitiesServiceImpl implements ICitiesService {

    @Autowired
    private CitiesMapper citiesMapper;

    @Override
    public CitiesOutDTO listProvinces(Integer[] requestCityInfo) {
        CitiesOutDTO citiesOutDTO = new CitiesOutDTO();

        List<CityOutDTO> provinces = citiesMapper.listByParentId(1);
        List<CityOutDTO> cities = citiesMapper.listByParentId(requestCityInfo[0]);
        List<CityOutDTO> counties = citiesMapper.listByParentId(requestCityInfo[1]);

        citiesOutDTO.setProvinces(provinces);
        citiesOutDTO.setCities(cities);
        citiesOutDTO.setCounties(counties);

        return citiesOutDTO;
    }

    @Override
    public CitiesOutDTO listByParentId(Map<String, Integer> citiesInfo) {
        CitiesOutDTO citiesOutDTO = new CitiesOutDTO();
        if(citiesInfo.get("cityId") != null) {
            List<CityOutDTO> citiesList = citiesMapper.listByParentId(citiesInfo.get("cityId"));
            citiesOutDTO.setCounties(citiesList);
        } else {
            List<CityOutDTO> cityOutDTOList = citiesMapper.listByParentId(citiesInfo.get("provinceId"));
            citiesOutDTO.setCities(cityOutDTOList);
            List<CityOutDTO> cityOutDTOList1 = citiesMapper.listByParentId(cityOutDTOList.get(0).getId());
            citiesOutDTO.setCounties(cityOutDTOList1);
        }
        return citiesOutDTO;
    }
}
